package com.luyuanhe.controller;

import com.luyuanhe.common.vo.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{userId}/{productId}/{amount}")
    public ResultMessage purchaseProduct(@PathVariable("userId") Long userId,
                                         @PathVariable("productId") Double productId,
                                         @PathVariable("amount") Double amount,
                                         HttpServletRequest request) {
        log.info("扣除产品余额");
        String url = "http://fund-service/fund/account/balance/{userId}/{amount}";
        Map<String,Object> params = new HashMap<>(2);
        params.put("userId",userId);
        params.put("amount",amount);
        ResultMessage resultMessage = restTemplate.postForObject(url, null, ResultMessage.class, params);
        log.info("respone:{}",resultMessage.getData());
        ResultMessage result = new ResultMessage("200", "交易成功！");
        return result;
    }
}
